package com.jooq.example.controller;

import com.jooq.example.domain.tables.pojos.SysDept;
import com.jooq.example.dto.PageModel;
import com.jooq.example.service.SysDeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StopWatch;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
public class SysDeptController {

    @Autowired
    private SysDeptService sysDeptService;

    @PostMapping(value = "save")
    public String save(@Validated SysDept sysDept, BindingResult br) {
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        log.info("开始执行 save 方法，入参：{}", sysDept.toString());
        if (br.hasErrors()) {
            log.info("参数校验异常：{}");
        }
        sysDeptService.save(sysDept);
        stopWatch.stop();
        log.info("save 方法执行结束，共耗时：{}", stopWatch.getTotalTimeSeconds());
        return "success";
    }

    @PutMapping(value = "update")
    int update(@Validated SysDept sysDept, BindingResult br) {
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        log.info("开始执行 update 方法，入参：{}", sysDept.toString());
        if (br.hasErrors()) {
            log.info("参数校验异常：{}");
        }
        sysDeptService.updateWithSelect(sysDept);
        stopWatch.stop();
        log.info("update 方法执行结束，共耗时：{}", stopWatch.getTotalTimeSeconds());
        return 1;
    }

    @DeleteMapping(value = "delete")
    int delete(Integer id) {
        return 1;
    }

    @GetMapping(value = "list")
    List<SysDept> queryList(SysDept sysDept) {
        return sysDeptService.list(sysDept);
    }

    @GetMapping(value = "listm")
    List<SysDept> queryListm(SysDept sysDept) {
        return sysDeptService.listm(sysDept);
    }

    @GetMapping(value = "page")
    List<SysDept> pageList(SysDept sysDept, PageModel pageModel) {
        return sysDeptService.pageList(sysDept, pageModel);
    }

}
